﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using AnesSystem.Util;
using System.Collections.ObjectModel;
using AnesSystem.Controls;

namespace AnesSystem.ViewModels
{
    public class SecondMenuListCtrlViewModel:ViewModel
    {
        #region Variables

        private ObservableCollection<SecondLevelMenuBtn> menuListCtrl;
        private List<MenuListModel> menulist ;

        #endregion

        #region Constructor

        public SecondMenuListCtrlViewModel()
        {
            Initial();
        }

        #endregion

        #region Properties

        public ObservableCollection<SecondLevelMenuBtn> MenuListCtrl
        {
            get
            {
                return menuListCtrl;
            }

            set
            {
                menuListCtrl = value;
                RaisePropertyChanged("MenuListCtrl");
            }
        }

        public SecondLevelMenuBtn SelectedMenu { get; set; }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        public event EventHandler<MenuListModel> Click;

        #endregion

        #region Private Methods

        private void Initial()
        {
            menuListCtrl = new ObservableCollection<SecondLevelMenuBtn>();
            menulist = new List<MenuListModel>();
            InitialMenuList();
            SetSelected(menulist.OrderBy(O => O.Order).FirstOrDefault());
        }

        #endregion

        #region Public/Protected Methods

        public void InitialMenuList()
        {
            MenuListCtrl.Clear();
            menulist= DataManager.Instance.MenuList.Where(O => O.Parent == FirstLevelMenuListCtrlViewModel.Instance.SelectedMenu.Index).OrderBy(O => O.Order).ToList();
            foreach (var item in menulist)
            {
                SecondLevelMenuBtn ctrl = new SecondLevelMenuBtn();
                ctrl.Margin = new System.Windows.Thickness(62, 0, 0, 0);
                ctrl.DataContext = item;
                ctrl.Click += Ctrl_Click;
                MenuListCtrl.Add(ctrl);
            }
        }

        public void SetSelected(MenuListModel menu)
        {
            foreach (var item in MenuListCtrl)
            {
                if(menu.Id.Equals((item.DataContext as MenuListModel).Id))
                {
                    item.IsChecked = true;
                    SelectedMenu = item;
                }
                else
                {
                    item.IsChecked = false;
                }
            }
        }

        #endregion

        #region EventHandlers

        private void Ctrl_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SecondLevelMenuBtn btn = sender as SecondLevelMenuBtn;
            MenuListModel selBtn = btn.DataContext as MenuListModel;
            if (SelectedMenu == btn)
            {

            }
            else
            {
                SelectedMenu = btn;
                btn.IsChecked = !btn.IsChecked;
                foreach (SecondLevelMenuBtn item in MenuListCtrl)
                {
                    if (!(item.DataContext as MenuListModel).Id.Equals(selBtn.Id))
                    {
                        item.IsChecked = !btn.IsChecked;
                    }
                }
            }
            Click?.Invoke(this, selBtn);
        }

        #endregion
    }
}
